<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改积分</block>
		</cu-custom>
		<view class="f40">
			<view class="m-all24 p-all24 bg-white border-r">
				<text>当前积分:</text>
				<text>{{ sum }}</text>
			</view>
			<view class="m-all24 p-all24 bg-white border-r flex justify-between align-center">
				<text>修改类型:</text>
				<radio-group class="ml20 flex-center" @change="changeType">
					<label class="radio flex-center mr20">
						<radio value="1" :checked="value == '1'" class="mr10" />
						<text>增加</text>
					</label>
					<label class="radio flex-center mr20">
						<radio value="2" :checked="value == '2'" class="mr10" />
						<text>减少</text>
					</label>
				</radio-group>
			</view>
			<view class="m-all24 p-all24 bg-white border-r flex justify-between align-center">
				<text>修改积分:</text>
				<input class="text-right" type="number" v-model="num" placeholder="请输入修改的积分" />
			</view>
			<button class="m-all24 border-r" @tap="submit" :class="'bg-' + theme.backgroundColor">
				提交
			</button>
		</view>
	</view>
</template>
<script>
const app = getApp();
const util = require("utils/util.js");
import api from "utils/api";

export default {
	data() {
		return {
			theme: app.globalData.theme,
			type: "",
			num: "",
			sum: "",
			userId: "",
		};
	},
	methods: {
		changeType(e){
			this.type = e.detail.value;
		},
		submit() {
			uni.showModal({
				title: "提示",
				content: "确定修改吗?",
				showCancel: true,
				success: ({ confirm, cancel }) => {
					//正整数
					if (!/^[1-9]\d*$/.test(this.num)) {
						uni.showToast({
							title: "请输入正整数",
							icon: "none",
						});
						return;
					}
					let pointChang = this.type == "1" ? this.num : -this.num;
					let para = {
						id: this.userId,
						pointChang,
					};
					api.editPoints(para).then((res) => {
						this.num = "";
						this.type = "";
						uni.showToast({
							title: "修改成功",
							icon: "none",
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1,
							});
						}, 1000);
					});
				},
			});
		},
	},
	onLoad(options) {
		this.sum = options.sum;
		this.userId = options.id;
	},
};
</script>
<style lang="scss" scoped></style>
